<?php

declare(strict_types=1);

namespace App\VO;

use App\VO\Interfaces\IVODetail;
use App\VO\Interfaces\IVOList;
use App\VO\Interfaces\IVOListItem;

abstract class AbstractBuilder
{
    public const NAMESPACE_SEPERATOR = '\\';
    public const BASE_TYPES          = ['int', 'string', 'array'];
    protected static $namespace;
    protected static $listClass;
    protected static $listItemClass;
    protected static $detailClass;
    protected static $complexClasses;

    //创建 LIST
    public static function buildList(array $list): IVOList
    {
        $voList = [];
        foreach ($list as $item) {
            $voList[] = self::buildListItem($item);
        }
        $voListClass = self::getVOListClass();
        return new $voListClass($voList);
    }

    //创建 LIST ITEM
    public static function buildListItem($item): IVOListItem
    {
        $voListItemClass = self::getVOListItemClass();

        return new $voListItemClass($item);
    }

    //创建 DETAIL
    public static function buildDetail(array $detail): IVODetail
    {
        $voDetailClass = self::getVODetailClass();
        return new $voDetailClass($detail);
    }

    public static function buildComplex(array $data): array
    {
        $classList        = [];
        $voComplexClasses = self::getVOComplexClasses();
        foreach ($voComplexClasses as $column => $className) {
            $classList[$column] = self::isBaseDataType($className) ? self::genDataTypeDefaultValue($data[$column], $className) : new $className($data[$column]);
        }

        return $classList;
    }

    //设定 NAMESPACE
    protected static function setNamespace(string $namespace)
    {
        self::$namespace = $namespace . self::NAMESPACE_SEPERATOR;
    }

    //设定 LIST 类
    protected static function setListClass(string $className)
    {
        self::$listClass = $className;
    }

    //设定 LIST ITEM 类
    protected static function setListItemClass(string $className)
    {
        self::$listItemClass = $className;
    }

    //设定 DETAIL 类
    protected static function setDetailClass(string $className)
    {
        self::$detailClass = $className;
    }

    //设定 COMPLEX
    protected static function setComplexClasses(array $classNames)
    {
        // self::$complexClasses = $classNames;
        $classes = [];
        foreach ($classNames as $column => $className) {
            self::$complexClasses[$column] = self::isBaseDataType($className) ? $className : self::$namespace . $className;
        }
    }

    //获取 LIST 类
    protected static function getVOListClass()
    {
        return self::$namespace . self::$listClass;
    }

    //获取 LIST ITEM 类
    protected static function getVOListItemClass()
    {
        return self::$namespace . self::$listItemClass;
    }

    //获取 DETAIL 类
    protected static function getVODetailClass()
    {
        return self::$namespace . self::$detailClass;
    }

    protected static function getVOComplexClasses()
    {
        return self::$complexClasses;
    }

    private static function genDataTypeDefaultValue($data, $dataType)
    {
        switch ($dataType) {
            case 'int':
                return intval($data);
                break;
            case 'string':
                return strval($data);
                break;
            case 'array':
                return (array) $data;
                break;
            default:
                return null;
                break;
        }
    }

    private static function isBaseDataType($dataTypeName): bool
    {
        return in_array($dataTypeName, self::BASE_TYPES);
    }
}
